﻿#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
	Mat src = imread("bloom.png");//打开图片

	if (src.empty())
	{
		printf("could not load image....\n");
		return -1;
	}
	namedWindow("input", WINDOW_AUTOSIZE);
	imshow("input", src);

	int height = src.rows;	//读图片高度
	int width = src.cols;	//读取图片宽度
	int ch = src.channels();	//读取图片通道数 rgb：三通道
	int r, g, b, x, y, axis;
	short cl;
	ofstream outfile;
	outfile.open("LCD3.asm");//输出代码文件

	outfile << ".text" << endl;
	//数组读取图像像素
	for (int row = 0; row < height; row++)
	{
		for (int col = 0; col < width; col++)
		{
			if (ch == 3)
			{
				Vec3b bgr = src.at<Vec3b>(row, col);
				b = bgr[0] & 0xff;//蓝 
				g = bgr[1] & 0xff;//绿
				r = bgr[2] & 0xff;//红
				x = col, y = row;
				axis = (0x7f & y) | ((0x7f & x) << 7);

				r >>= 3, g >>= 3, b >>= 3;

				cl = (short)(r << 10 | g << 5 | b);
				outfile << "addi $s5 $0 " << axis << endl;//x,y

				outfile << "addiu $s6 $s5 " << cl << endl;//rgb555

			}
		}
	}
	
	outfile << "addi $v0 $0 10"<< endl;
	outfile << "syscall" << endl;
	
	outfile.close();
	return 0;
}
